package com.yeren.weixin.gzh.weixin.operation;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yeren.weixin.gzh.weixin.menu.Button;
import com.yeren.weixin.gzh.weixin.menu.ClickButton;
import com.yeren.weixin.gzh.weixin.menu.ComplexButton;
import com.yeren.weixin.gzh.weixin.menu.Menu;
import com.yeren.weixin.gzh.weixin.menu.ViewButton;

public class MenuManagerExecute {
	private static Logger log = LoggerFactory.getLogger(MenuManagerExecute.class);
	// 菜单创建（post）
	public final static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	// 菜单查询（get）
	public final static String menu_get_url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
	// 菜单删除（get）
	public final static String menu_delete_url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";

	public static Menu initMenu() {
		// 第一个大按钮
		ClickButton btn11 = new ClickButton();
		btn11.setName("百度");
		btn11.setType("click");
		btn11.setKey("baidu");

		ClickButton btn12 = new ClickButton();
		btn12.setName("我维护的公众号");
		btn12.setType("click");
		btn12.setKey("smygzh");

		ClickButton btn13 = new ClickButton();
		btn13.setName("授权");
		btn13.setType("click");
		btn13.setKey("oauth2");

		ClickButton btn14 = new ClickButton();
		btn14.setName("js_SDK");
		btn14.setType("click");
		btn14.setKey("js");

		ClickButton btn15 = new ClickButton();
		btn15.setName("firefly");
		btn15.setType("click");
		btn15.setKey("LTZ");

		// 第二个大按钮
		ViewButton btn21 = new ViewButton();
		btn21.setName("上海天意庇哩亚教会");
		btn21.setType("view");
		btn21.setUrl("http://yeren108.duapp.com/yeren-ty-website/");

		ViewButton btn22 = new ViewButton();
		btn22.setName("boss登陆");
		btn22.setType("view");
		btn22.setUrl("http://yeren108.duapp.com/weixinty/login.jsp");

		ViewButton btn23 = new ViewButton();
		btn23.setName("csdn|code");
		btn23.setType("view");
		btn23.setUrl("https://code.csdn.net/yeren108");

		ViewButton btn24 = new ViewButton();
		btn24.setName("市民云下载");
		btn24.setType("view");
		btn24.setUrl("http://app.eshimin.com/download.html");

		ViewButton btn25 = new ViewButton();
		btn25.setName("cms后台");
		btn25.setType("view");
		btn25.setUrl("http://yeren108.duapp.com/yeren-cms/crud/article/articleList.jsp");

		// 第三个大按钮
		ViewButton btn31 = new ViewButton();
		btn31.setName("市民云");
		btn31.setType("view");
		btn31.setUrl("http://app.eshimin.com/download.html");

		ViewButton btn32 = new ViewButton();
		btn32.setName("市民信箱");
		btn32.setType("view");
		btn32.setUrl("https://www.eshimin.com/");

		ViewButton btn33 = new ViewButton();
		btn33.setName("百度");
		btn33.setType("view");
		btn33.setUrl("http://www.baidu.com");

		ViewButton btn34 = new ViewButton();
		btn34.setName("csdn|code");
		btn34.setType("view");
		btn34.setUrl("https://code.csdn.net/yeren108");

		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("研究");
		mainBtn1.setSub_button(new Button[] { btn11, btn12, btn15 });

		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("技术交流");
		mainBtn2.setSub_button(new Button[] { btn21, btn25 , btn23});

		ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("常用链接");
		mainBtn3.setSub_button(new Button[] { btn31, btn32});

		Menu menu = new Menu();
		menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });

		return menu;
	}

	/**
	 * 创建菜单
	 * 
	 * @param menu
	 *            菜单实例
	 * @param accessToken
	 *            凭证
	 * @return true | false
	 */
	public static boolean createMenu(Menu menu, String accessToken) {
		boolean result = false;
		String url = menu_create_url.replace("ACCESS_TOKEN", accessToken);
		// 将菜单对象转换成JSON字符串
		String jsonMenu = JSONObject.fromObject(menu).toString();
		// 发起POST请求创建菜单
		JSONObject jsonObject = CommonExecute.httpsRequest(url, "POST", jsonMenu);

		if (null != jsonObject) {
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			if (0 == errorCode) {
				result = true;
			} else {
				result = false;
				log.error("创建菜单失败errcode:"+errorCode+"        errmsg:"+errorMsg);
			}
		}
		return result;

	}

	/**
	 * 查询菜单
	 * 
	 * @param accessToken
	 * @return String
	 */
	public static String getMenu(String accessToken) {
		String result = null;
		String requestUrl = menu_get_url.replace("ACCESS_TOKEN", accessToken);
		// 发起GET请求查询菜单
		JSONObject jsonObject = CommonExecute.httpsRequest(requestUrl, "GET", null);
		if (null != jsonObject) {
			result = jsonObject.toString();
		}
		return result;
	}

	/**
	 * 删除菜单
	 * 
	 * @param accessToken
	 * @return true | false
	 */
	public static boolean deleteMenu(String accessToken) {
		boolean result = false;
		String requestUrl = menu_delete_url.replace("ACCESS_TOKEN", accessToken);
		// 发起GET请求删除菜单
		JSONObject jsonObject = CommonExecute.httpsRequest(requestUrl, "GET", null);

		if (null != jsonObject) {
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			if (0 == errorCode) {
				result = true;
			} else {
				result = false;
				log.error("删除菜单失败 errcode:"+ errorCode+"        errmsg:"+errorMsg);
			}
		}
		return result;
	}
}
